﻿using System;
using System.Collections.Generic;
using System.Text;
using Application.Domain;
using Evaluant.Uss.ObjectContext;

namespace Application.Services
{
    public class EussService : IService
    {
    protected ObjectContext oc;

    public EussService(ObjectContext oc)
    {
        this.oc = oc;
    }

        public Person GetPersonById(string id)
        {
            return oc.LoadWithId<Person>(id);
        }

        public bool DeletePerson(string id)
        {
            Person p = oc.LoadWithId<Person>(id);

            if (p == null)
                return false;

            try
            {
                oc.BeginTransaction();
                oc.Delete(p);
                oc.CommitTransaction();
            }
            catch
            {
                return false;
            }

            return true;
        }

    public string CreatePerson(string id, string firstname, string lastname)
    {
        Person p = new Person();

        p.Firstname = firstname;
        p.Lastname = lastname;

        try
        {
            oc.BeginTransaction();
            oc.Serialize(p);
            oc.CommitTransaction();
        }
        catch
        {
            oc.RollBackTransaction();
            return String.Empty;
        }

        return p.Id;
    }

        public bool UpdatePerson(string id, string firstname, string lastname)
        {
            Person p = oc.LoadWithId<Person>(id);

            if (p == null)
                return false;

            p.Firstname = firstname;
            p.Lastname = lastname;

            try
            {
                oc.BeginTransaction();
                oc.Serialize(p);
                oc.CommitTransaction();
            }
            catch
            {
                oc.RollBackTransaction();
                return false;
            }

            return true;
        }

        public IList<Person> GetAllPerson()
        {
            Query q = new Query(typeof(Person));
            return oc.Load<Person>(q);
        }

        public IList<Person> GetAllPerson(string sortExpression, int firstResult, int maxResults)
        {
            Query q = new Query(typeof(Person));
            q.OrderBy.Add(sortExpression);
            q.MaxResults = maxResults;
            q.FirstResult = firstResult + 1;

            return oc.Load<Person>(q);
        }

        public int GetPersonCount()
        {
            return (int)oc.LoadScalar("count(Domaine:Person)");
        }

        public void Initialize()
        {
            oc.InitializeRepository();

            oc.BeginTransaction();

            for (int i = 0; i < 100; i++)
            {
                Person p1 = new Person();
                p1.Firstname = "Sébastien + " + i;
                p1.Lastname = "Ros" + i;
                oc.Serialize(p1);
            }

            oc.CommitTransaction();
        }
    }
}
